package IO;

import java.io.*;

public class InputStreamReader_ {
    public static void main(String[] args) {
        File file = new File("test.txt");
        File file1 = new File("test-转换流-gbk.txt");
        File file2 = new File("test-转换流-utf.txt");

        System.out.println();
//        UTFConversionGBK(file,file1);
        GBKConversionUTF(file1,file2);

    }

    private static void GBKConversionUTF(File file1, File file2) {
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;
        try {
            FileInputStream fileInputStream = new FileInputStream(file1);
            FileOutputStream outputStream = new FileOutputStream(file2);
            isr = new InputStreamReader(fileInputStream,"GBK");
            osw = new OutputStreamWriter(outputStream,"UTF-8");

            int len = 0;
            char[] chars = new char[10];
            while ((len = isr.read(chars))!=-1){
                osw.write(chars,0,len);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (isr!=null){
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (osw!=null){
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void UTFConversionGBK(File file, File file1) {

        InputStreamReader isr = null;
        OutputStreamWriter osw = null;



        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            FileOutputStream outputStream = new FileOutputStream(file1);
            isr = new InputStreamReader(fileInputStream,"UTF-8");
            osw = new OutputStreamWriter(outputStream,"GBK");

            int len = 0;
            char[] chars = new char[10];
            while ((len = isr.read(chars))!=-1){
                osw.write(chars,0,len);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (isr!=null){
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (osw!=null){
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
